#include "OptionTools.h"
#include "IOptionUi.h"
#include "Tools.h"
#include "OptCtrlTools.h"
#include "OptUiTools.h"

const bool COptionTools::Init(const void * pvdPara, char * pszMsgTxt, int iMsgTxtSize)
{	
	ValueSet().SetValue(MAIN_FORM, NULL);
	ValueSet().SetValue(CONTRACT_FORM, NULL);
	ValueSet().SetValue(SUBJECT_MONITOR_FORM, NULL);
	ValueSet().SetValue(SUBJECT_ORDER_MATHING_FORM, NULL);
	CREATE_STK_TRD_CTRL(m_ptrStkTrdCtrl)
	CREATE_CONTRACT_COLUMN_TITLE(m_ptrContractColumnTitle)
	CREATE_SUBJECT_GUI(m_ptrSubjectGui)
	CREATE_CALCULATE_CONTRACT_QUOTATION(m_ptrCalculateContractQuotation)
	m_ptrClientCheckRisk = Invoke<IClientCheckRisk>("CClientCheckRisk", nullptr, nullptr, 0);
	glbOptCtrTools = this;
	glbOptUiTools = Instance<IOptionUiTools>("COptionUiTools", nullptr, nullptr, 0);
	m_bInitFlg = m_ptrStkTrdCtrl != nullptr && 
		         m_ptrContractColumnTitle != nullptr &&
		         m_ptrSubjectGui != nullptr &&
		         m_ptrClientCheckRisk != nullptr &&
		         m_ptrCalculateContractQuotation != nullptr;
	return m_bInitFlg;
}

bool COptionTools::CalculateFlg(const char * pszLbm, IValueSet * ptrValList, IPacket ** ptrPktJson)
{
	bool bRetVal = false;
	try
	{
		if (Kernel().StopFlg()) goto __end;
		if (!ptrValList) goto __end;
		string strName(pszLbm + string("_Empty"));
		if (!ptrValList->GetValue(strName.c_str(), bRetVal)) goto __end;
		if (bRetVal)  bRetVal = false;
		else
		{
			string strJson("");
			strName = pszLbm + string("__JsonResult");
			if (!ptrValList->GetValue(strName.c_str(), strJson)) goto __end;
			CREATE_PTR_JSON_PACKET_PARA(ptrRetVal, strJson.c_str());
			(*ptrPktJson) = ptrRetVal;
			bRetVal = true;
		}
	}
	catch (...)
	{

	}
__end:
	return bRetVal;
}

void COptionTools::VarFromJson(const char * pszLbm, IValueSet * ptrValList, void * ptrRecInfo, IPacket * ptrPktJson, void * ptrFieldNameList, void * ptrResult, const char * pszKey)
{
	bool bFlg = false;
	string strRetVal(""), strList(""), strHisVal;
	const char * pszCurVal = nullptr;
	IValueSet * ptrResultList = &ptrValList->List("Result");
	IValueSet * ptrCare = &ptrValList->List("Care");
	vector<string> &FieldNameList = *reinterpret_cast<vector<string> *>(ptrFieldNameList);
	ptrCare->DelVar("");
	for (size_t i = 0; i < FieldNameList.size(); i++)
	{
		pszCurVal = ptrPktJson->GetFieldValueAsString(ptrRecInfo, FieldNameList[i].c_str());
		if (!ptrResultList->GetValue(FieldNameList[i].c_str(), strHisVal)) continue;
		bFlg = strcmp(strHisVal.c_str(), pszCurVal) == 0;
		if (bFlg) continue;
		ptrResultList->SetValue(FieldNameList[i].c_str(), string(pszCurVal));
		strList += FieldNameList[i] + "*";
	}
	AddResultItem(ptrResult, pszLbm, pszKey, "", strList.c_str(), "");
	return;
}

void COptionTools::AddResultItem(void * ptrResult, const char * pszLbm, const char * pszKey, const char * pszFlg, const char * pszName, const char * pszValue)
{
	vector<string> &Result = *reinterpret_cast<vector<string> *>(ptrResult);
	Result.push_back(string(pszLbm + string("||") + pszName + string("||") + pszValue + string("||") + pszFlg + string("||") + pszKey));
}

void COptionTools::PushTask(bool bCondition, void * ptrResult)
{
	if (!ptrResult) goto __end;
	try
	{
		vector<string> vtTask;
		shared_ptr<IRefreshUi> ptrRefreshUi = nullptr;
		char szName[CHAR_LEN_64] = { 0 };
		int iCount = FrameTools().StrFun().SeparateCount(REFRESH_CLS_NAME, "|");
		vector<string> &Result = *reinterpret_cast<vector<string> *>(ptrResult);
		for (auto it : Result)
			for (size_t i = 1; i <= iCount; i++)
			{
				FrameTools().StrFun().SeparateString(szName, sizeof(szName), REFRESH_CLS_NAME, "|", i);
				ptrRefreshUi = Invoke<IRefreshUi>(szName, nullptr, nullptr, 0);
				if (ptrRefreshUi == nullptr) continue;
				ptrRefreshUi->UpdateFlag(it.c_str(), reinterpret_cast<void *>(&vtTask));
				ptrRefreshUi->AddTask(reinterpret_cast<void *>(&vtTask));
				vtTask.clear();
			}
		Result.clear();
	}
	catch (...)
	{

	}

__end:
	return;
}

IStkTrdCtrl & COptionTools::StkTrdCtrl()
{
	return *m_ptrStkTrdCtrl.get();
}

IContractColumnTitle & COptionTools::ContractColumnTitle()
{
	return *m_ptrContractColumnTitle.get();
}

ISubjectGui & COptionTools::SubjectGui()
{
	return *m_ptrSubjectGui.get();
}

IClientCheckRisk & COptionTools::ClientCheckRisk()
{
	return *m_ptrClientCheckRisk.get();
}

ICalculateContractQuotation & COptionTools::CalculateContractQuotation()
{
	return *m_ptrCalculateContractQuotation.get();
}



